import g_get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import render from '@dfeidao/atom-web/render/render';

import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
import p004 from './p004';
import p006 from './p006';
interface IReply {
	comment_numbering: string;
	productid: string;
	reply_time: number;
	reply_time_str: string;
	dynamic_id: string;
	reply_content: string;
	respondent_numbering: string;
	reply_numbering: string;
	staff_name: string;
	headimg: string;
	statistical_magnitude: number;
	isa: number;
	auth_code: number;
}
export default async function a014(fd: IFeidaoAiBrowserComponent) {
	// 评论回复
	const page_name = fd.data.node.getAttribute('page-name');
	const dynamic_id = get(fd, 'dynamic_id');
	const productid = get(fd, 'productid');
	const staff_no = get(fd, 'staff_no');
	const comment_numbering = get(fd, 'comment_numbering');
	let page_no = 1;
	if (!page_no) {
		page_no = 1;
	}
	let page_size = Number(get(fd, 'page_size'));
	if (!page_size) {
		page_size = 5;
	}
	let auth_code = '';
	if (page_name === 'depart-details') {
		auth_code = g_get('depart_details_auth_code');
	} else if (page_name === 'sign-details') {
		auth_code = g_get('sign_details_auth_code');
	} else if (page_name === 'grab-details') {
		auth_code = g_get('grab_details_auth_code');
	} else if (page_name === 'lottery-details') {
		auth_code = g_get('lottery_details_auth_code');
	}
	const query = {
		dynamic_id,
		comment_numbering,
		page_size,
		page_no,
		employee_no: staff_no,
		auth_code,
		productid
	};
	set(fd, 'page_no_reply', page_no);
	try {
		show_loading();
		const result = await nodejs<{ dataArray: IReply[], count: number, page_no: number, page_size: number }>(page_name + '/s005', query);
		hide_loading();
		render(fd, result.dataArray, p004, 'p000' + comment_numbering, 'inner');
		if (result.count <= query.page_no * query.page_size) {
			fd.data.node.querySelector<HTMLDivElement>('[comment_numbering="' + comment_numbering + '"').classList.add('display-n');
		}
		let open = '[data-comment="open-review-';
		open = open + comment_numbering;
		open = open + '"]';
		fd.data.node.querySelector<HTMLDivElement>(open).classList.add('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-comment="close-review-' + comment_numbering + '"]').classList.remove('display-n');
		for (let i = 0; i < result.dataArray.length; i++) {
			render(fd, result.dataArray[i], p006, 'p006' + result.dataArray[i].reply_numbering, 'inner');
		}
	} catch (e) {
		hide_loading();
		error('操作失败');
	}
}
